package controll;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.rmi.activation.ActivationGroup_Stub;

@MultipartConfig(location = "e:\\aa")
@WebServlet("/fileUpload")
public class FileUpload extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
//        for (Part part: request.getParts()){
//            if (part.getName().startsWith("file")){
//                String filename=getFilename(part);
//                part.write(filename);
//        }
        Part part=request.getPart("file");//取出文件名
        String header=part.getHeader("Content-Disposition");
        int start=header.lastIndexOf("=\"");
        String filename=header.substring(start+2);
        filename=filename.substring(0,filename.length()-1);
        System.out.println(filename);
        part.write(filename);
//    request.setCharacterEncoding("utf-8");
//        BufferedReader br=request.getReader();
//        String s=br.readLine();
//        while (s==null){
//            System.out.println(s);
//            s=br.readLine();
//        }
//        br.close();
//        File file=new File("e:\\aa\\xx.doc");
//        BufferedWriter bw=new BufferedWriter(new FileWriter(file));

//    ServletInputStream sis=request.getInputStream();
//    byte[] tem=new byte[8192];
//        File file=new File("192.168.1.108\\e:\\aa\\dc.rar");
//        FileOutputStream fos=new FileOutputStream(file);
//    int a=sis.read(tem);
//    while (a!=-1){
//        fos.write(tem,0,a);
//        a=sis.read(tem);
//    }

//       fos.close();
        PrintWriter out=response.getWriter();
        out.write("success");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
